import axios from 'axios'

//喧染页面结果
function renderFunc(data,tableObj) {
  tableObj.tableData = data
  // console.log(tableData)
  tableObj.number = tableObj.tableData.length>=6?6:tableObj.tableData.length === 0?1:tableObj.tableData.length
  tableObj.totalPages = tableObj.tableData.length%tableObj.number===0?tableObj.tableData.length/tableObj.number:tableObj.tableData.length/tableObj.number+1
  tableObj.totalPages = parseInt(tableObj.totalPages)
  tableObj.temporary = tableObj.tableData.slice((tableObj.currentPage - 1) * tableObj.number, (tableObj.currentPage - 1) * tableObj.number + tableObj.number)
  return tableObj
}

//  get请求
function getRequest(path,tableObj) {
    const p = new Promise( (resolve,reject)=>{
        axios.get(path).then(value=>{resolve(value)},reason=>{reject(reason)})
     })
     p.then(value=>{
       // console.log(value)
     renderFunc(value.data,tableObj)
       },reason=>{
       console.log(reason)
     })
}

//  post请求
function postRequest(path,tableObj) {
    const p = new Promise( (resolve,reject)=>{
        axios.post(path).then(value=>{resolve(value)},reason=>{reject(reason)})
     })
     p.then(value=>{
       // console.log(value)
       renderFunc(value.data,tableObj)
       },reason=>{
       console.log(reason)
     })
}

//改变一页有多少个元素
function changeNumber(tableObj){
    // console.log('数字发生改变:',number)
    tableObj.totalPages = tableObj.tableData.length%tableObj.number===0?tableObj.tableData.length/tableObj.number:tableObj.tableData.length/tableObj.number+1
  tableObj.totalPages = parseInt(tableObj.totalPages)
   tableObj.currentPage = tableObj.currentPage>tableObj.totalPages?tableObj.totalPages:tableObj.currentPage
    // tableData.slice(tableData.length)
    tableObj.temporary = tableObj.tableData.slice((tableObj.currentPage-1)*tableObj.number,(tableObj.currentPage-1)*tableObj.number+tableObj.number)
  // tableObj.number = tableObj.temporary.length>3?3:tableObj.temporary.length   
  return tableObj

}

//修该当前第几页
 function currentChange(val,tableObj){
    // console.log( tableObj.currentPage,val)
    tableObj.currentPage = val
    tableObj.temporary = tableObj.tableData.slice((tableObj.currentPage-1)*tableObj.number,(tableObj.currentPage-1)*tableObj.number+tableObj.number)
    return tableObj

}

//首页
function getFirstPage(tableObj){
    tableObj.temporary = tableObj.tableData.slice(0,tableObj.number)
    tableObj.currentPage=1
    return tableObj
}

//末页
function getLastPage(tableObj){
    var first = tableObj.tableData.length%tableObj.number === 0 ? tableObj.tableData.length-tableObj.number :parseInt(tableObj.tableData.length/tableObj.number)*tableObj.number
    tableObj.temporary = tableObj.tableData.slice(first)
    tableObj.currentPage = tableObj.tableData.length%tableObj.number === 0 ? tableObj.tableData.length/tableObj.number :parseInt(tableObj.tableData.length/tableObj.number)+1
    return tableObj
}

//判断是否与登录
function isLogin(that) {
  if (that.$cookie.get("userId")) {
    return true
  } else {
    return false
  }
}

//查询一种茶叶的库存量
async function oneTeaStore(url,teaId) {
  let result
  const p = new Promise((resolve, reject) => {
    axios.get('http://localhost:3000/' + url + teaId).then(response => {
      resolve(response)
    }, error => {
      reject(error)
    })
  })
  await p.then(response => {
    result = response
  }, error => {
    result =error
  })
  return result

}

export {
  renderFunc,
    getRequest,
    postRequest,
    changeNumber,
  currentChange,
  getFirstPage,
  getLastPage,
  isLogin,
  oneTeaStore
}
